
var Class = require('ClassBuilder');

var keys = require('./keys.js');

var RSAHelper = require('../../encrypt/RSAHelper');
var AESHelper = require('../../encrypt/AESHelper');

var EncryptionTestProcesser = Class(function( _public ) {

    _public._construct = function( msgObj, socket ) {
        
        this.msgObj = msgObj;
        this.socket = socket;

    };

    _public.run = function() {

        var socket = this.socket;
        var msgObj = this.msgObj || {};

        if( !msgObj.content ) {
            socket.end();
            return;
        }


        var method  = msgObj.method == 'decrypt'? 'decrypt': 'encrypt' ;
        var content = msgObj.content;
        var result;

        if( msgObj.type == 'RSA' ) {

            var clientPem = keys.clientPem;
            var serverPem = keys.serverPem;

            var rsa = new RSAHelper( clientPem, serverPem );

            result = rsa[ method ]( content );

        }
        else if( msgObj.type == 'AES' ) {

            var AESKey = keys.AESKey;

            var aes = new AESHelper(AESKey);

            result = aes[ method ]( content );

        }

        socket.write(result);

    };

});

module.exports = EncryptionTestProcesser;